
package com.be.beadmin.product.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.product.dto.input.WrokingHoursDetailsDto;
import com.be.beadmin.product.dto.output.WrokingHoursDetailsEx;
import com.be.beadmin.product.entity.WrokingHoursDetails;
import com.be.beadmin.product.entity.WrokingHoursDetails_;
import com.be.beadmin.product.mapper.WrokingHoursDetailsMapper;
import com.be.beadmin.product.mapper.WrokingHoursDetailsMapperEx;
import com.be.beadmin.product.repository.WrokingHoursDetailsRepository;
import com.be.beadmin.product.service.WrokingHoursDetailsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-04-24
**/
@Service
@RequiredArgsConstructor
public class WrokingHoursDetailsServiceImpl implements WrokingHoursDetailsService {

    @Autowired
    private  WrokingHoursDetailsRepository wrokingHoursDetailsRepository;
    @Autowired
    private  WrokingHoursDetailsMapper wrokingHoursDetailsMapper;
    @Autowired
    private  WrokingHoursDetailsMapperEx wrokingHoursDetailsExMapper;

    @Override
    public Page<WrokingHoursDetailsEx> getWrokingHoursDetailsByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<WrokingHoursDetails> page = wrokingHoursDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(WrokingHoursDetails_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(WrokingHoursDetails_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<WrokingHoursDetailsEx> page1 = page.map(wrokingHoursDetailsExMapper::toEntity);
            return page1;

    }

    @Override
    public void addWrokingHoursDetails(WrokingHoursDetailsDto wrokingHoursDetailsDto) {
            WrokingHoursDetails result = wrokingHoursDetailsRepository.findWrokingHoursDetailsByIdAndDelete(wrokingHoursDetailsDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("WrokingHoursDetails不存在,请重新查询");
            }
            WrokingHoursDetails wrokingHoursDetails = wrokingHoursDetailsMapper.toEntity(wrokingHoursDetailsDto);
            wrokingHoursDetailsRepository.save(wrokingHoursDetails);
    }

    @Override
    public void updateWrokingHoursDetails(WrokingHoursDetailsDto wrokingHoursDetailsDto) {
            WrokingHoursDetails wrokingHoursDetails = wrokingHoursDetailsRepository.findWrokingHoursDetailsById(wrokingHoursDetailsDto.getId());
            wrokingHoursDetails = wrokingHoursDetailsMapper.toEntity(wrokingHoursDetailsDto);
            wrokingHoursDetailsRepository.save(wrokingHoursDetails);
    }


    @Override
    public void delWrokingHoursDetails(String id) {
        WrokingHoursDetails wrokingHoursDetails = wrokingHoursDetailsRepository.findWrokingHoursDetailsById(id);
        wrokingHoursDetails.setDelete(1);
        wrokingHoursDetailsRepository.save(wrokingHoursDetails);
    }

    @Override
    public void delWrokingHoursDetailss (List<String> ids) { }
}